import React from 'react'
// ===================================================================== Temp
import FormModal from '#cpt/temp/FormModal'
import Table from '#cpt/temp/Table'
import Input from '@antd/form/input'
import Select from '@antd/form/select'
import TableControls from '#cpt/temp/TableControls'
// ===================================================================== global declare
const { $fn, $Lazy,$http } = window
// ===================================================================== Lazy
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== data
const typeData = [{ name:'调入', value:'1' }, { name:'调出', value:'2' } ]
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		toalPrice:0,
		//修改账期
		formData:[
			{ label: '调整日期',		name: 'collection_date', 	type: 'date-time',format:1,  required:true, width:'33%' },
			{ label: '调整理由',		name: 'reason',required:true, width:'63%' },
			{ type:'cpt', full:true, component:()=>{
				return(
					<div className='ml10 r5px cf bcm plr10 lh30 tc mb10' style={{width:60}} onClick={()=>{
						let { data } = this.state
						data.forEach((v,i) => v.index = i)
						data.push({summary_sn:'',bill_price:'', index: data.length })
						this.setState({data:[...data]})
					}}>添加</div>
				)
			} },
			{ type:'cpt', full:true, component:()=>{
				return(
					<div className='fv w' style={{height:250}}>
						{ Table.call(this,{type:-1, isIndex:1, hasCurrent: false}) }
					</div>
				)
			} },
			// { label: '制表',		name: 'add_user_id', 	type: 'select', 	width:'30%',	loading:true, required:true },
			// { label: '审核',		name: 'audit_user_id', 	type: 'select', 	width:'30%',	loading:true, required:true },
		]
	}
	
	model = {}
	
	open = (data) => {
		this.modal.open({ formRef:this.formRef })
		this.getSelect(data)
	}

	// 获取下拉数据
	getSelect = (data) => {
		const { formData } = this.state
		$cache.then( async f => {
			// 医院名称
			const hospData = await f.getCache({ api:'hospital' } )
			// 区域
			const regionData = await f.getCache({ api:'bs_area' } )
			 // 账期
			const summaryData = await f.getCache({ api:'bill-summary/select' } )
			this.summaryData = summaryData
			this.setState({ formData, summaryData, hospData, regionData })
		})
	}
	
	// 表单
	cols = [
		{ title: '账期', 		dataIndex: 'summary_sn', 	width:150, render:(text,row)=>{
			let { summaryData } = this.state
			return <Select
				p = '请选择账期'
				data = {summaryData}
				onChange={ value =>{
					const hosp_id = row.hosp_id
					if(hosp_id)this.getBill({hosp_id,summary_sn:value})
					row['summary_sn'] = value
				}}
				// onDropdownVisibleChange = { bool =>{
				// 	if(bool){
				// 		let summaryData = $fn.copy( this.summaryData )
				// 		// let stack = []
				// 		data.forEach( v => {
				// 			summaryData.forEach( ({ value }, i) => {
				// 				if( value === v['summary_sn'] && value !== row['summary_sn'] ){
				// 					summaryData[i] = {}
				// 				}
				// 			})
				// 		})
				// 		summaryData = summaryData.filter( v => $fn.hasObject(v))
				// 		this.setState({ summaryData })
				// 	}
				// }}
			 />
		} },
		{ title: '受检单位', 	dataIndex: 'hosp_id', 			width:120, render:(text,row)=>{
			let { hospData } = this.state
			return <Select 
				p = '请选择受检单位'
				data = {hospData}
				onChange={ value =>{
					const summary_sn = row.summary_sn
					if(summary_sn)this.getBill({summary_sn,hosp_id:value})
					row['hosp_id'] = value
				}}
			/>
		} },
		{ title: '开票单位', 	dataIndex: 'organization_name', width:120, render:(text,row)=>{
			return <Input
				value = {row.organization_name}
				onChange = { value=>{
					row['organization_name'] = value
				} }
			 />
		} },
		{ title: '区域', 		dataIndex: 'region_id', 		width:100, render:(text,row)=>{
			let { regionData } = this.state
			return <Select 
				p = '请选择区域'
				value = {row.region_id}
				data = {regionData}
				onChange={ value =>{
					row['region_id'] = value
				}}
			/>
		}},
		{ title: '业务员', 		dataIndex: 'salesman_id', 		...$fn.table.name, render:(text,row)=>{
			let { salesmanData } = this.state
			return <Select
				p = '请选择业务员'
				data = {salesmanData}
				nameStr = 'real_name'
				idStr = 'salesman_id'
				onChange={ value =>{
					row['salesman_id'] = value
				}}
			 />
		} },
		{ title: '调整类型', 		dataIndex: 'type', 		width:100, render:(text,row)=>{
			// let { typeData } = this.state
			return <Select 
				p = '请选择调整类型'
				data = {typeData}
				onChange={ value =>{
					row['type'] = value
				}}
			/>
		}},
		{ title: '金额', 		dataIndex: 'collection_price', 	...$fn.table.num, render:(text,row)=>{
			return <Input 
				onChange={ value =>{
					row['collection_price'] = value
					
					this.setState(() => {
						const d = this.state.data.map( v => +v['collection_price'] ?? 0)
						return { toalPrice: d.reduce((t,v)=> t+v ) }
					})
					
					this.fromData = this.state.data.map( v => ({ 
						summary_sn:v.summary_sn, 
						collection_price:v.collection_price,
						salesman_id:v.salesman_id,
						region_id:v.region_id,
						organization_name:v.organization_name,
						hosp_id:v.hosp_id,
						type:v.type,
					}))
				}}
			/>
		} },
		TableControls.controls.call(this, { width: 80 })
	]
	
	// 表格操作按钮
	table_controls = (text, {index}) => {
		return [
			TableControls.del.call(this, { refresh:false,  onOk: (close) => {
				let { data } = this.state
				data.splice(index, 1)
				data.forEach((v,i) => v.index = i)
				this.setState({data:[...data]})
				close()
			} })
		]
	}
	
	getBill = param => {
		const { formData, data} = this.state
		$http.submit(null,'fi-standing-book/getHospSummaryPrice',{param:{...param, type:2}}).then(res=>{
			const {salesman,summary_sn,bill_info, hosp} = res
			const salesmanData = salesman
			data.forEach((v,i)=>{
				if(v.summary_sn === summary_sn.summary_sn){
					data[i].bill_price = bill_info.bill_price
					data[i].proceeds = bill_info.collection_price
					data[i].not_collection_price = bill_info.not_collection_price
					data[i].organization_name = hosp.organization_name
					data[i].region_id = hosp.region
				}
			})
			this.setState({ formData,data, salesmanData })
		})
	}
	
	render(){
		return FormModal.call(this,{ title:'填写回款调整单',width:'50%', api: 'fi-pay-book/addAdjustment',
			getSubmit: param => {
				param['from_data'] = this.fromData
				return param
			},
			close:()=>{
				this.setState({data:[]})
			}
		 
		})
	}
}

export default Index